/*
 *-----------------------------------------------------------------------------
 *
 * TIndexerRpcCaller.java
 *
 * This file was created from IIndexerRpc.idl
 * by the Simple JavRMI backend.
 *
 * Copyright 2008-2010, Evgeny Alexeyev
 * Licensed under the EPL - v1.0
 * http://www.eclipse.org/legal/epl-v10.html
 *
 *-----------------------------------------------------------------------------
 */

package com.googlecode.umlsynch.rpc;

import java.io.*;

import com.googlecode.umlsynch.rpc.base.TIOHandler;

import com.googlecode.umlsynch.rpc.base.IDebug;

import com.googlecode.umlsynch.rpc.base.TDebug;

import com.googlecode.umlsynch.rpc.TFileDescriptor;

import com.googlecode.umlsynch.rpc.TClassDescriptor;

import com.googlecode.umlsynch.rpc.IIndexerRpc;


public class TIndexerRpcCaller implements IIndexerRpc
{
  TIOHandler handler;
  TDebug debug;
  
  public TIndexerRpcCaller(TIOHandler hdl, IDebug dbg) {
    handler = hdl;
    debug = new TDebug(dbg);
  }
  
  @Override
  public TClassDescriptor[] GetClassRelations(TClassDescriptor desc, TRelationRpc relationType, TFileDescriptor[] searchRestrictions) {
    if (!handler.connect())
      return null;
    
    String data;
    try {
    
      handler.getObjectOutputStream().writeObject(new String("IIndexerRpc"));
      handler.getObjectOutputStream().writeObject(new String("GetClassRelations"));
      
      handler.getObjectOutputStream().writeObject(new String("TClassDescriptor[]"));
      handler.getObjectOutputStream().writeObject(desc);
      
      handler.getObjectOutputStream().writeObject(new String("TRelationRpc"));
      handler.getObjectOutputStream().writeObject(relationType);
      
      handler.getObjectOutputStream().writeObject(new String("TFileDescriptor[]"));
      handler.getObjectOutputStream().writeObject(searchRestrictions);
      
      TClassDescriptor[] result = null;
      data = handler.readString();
      if (data.equals("TClassDescriptor[]")) {
        result = (TClassDescriptor[]) handler.getObjectInputStream().readObject();
      } else {
        debug.Error("Recieved wrong parameter type:" + data + " expected TClassDescriptor[]");
        throw new ClassNotFoundException();
      }
      
      return result;
    }
    catch (IOException e) {
    }
    catch (ClassNotFoundException e) {
    }
    
    handler.close();
    return null;
  }; // GetClassRelations
  
  @Override
  public TClassDescriptor GetClassInfo(TClassDescriptor desc) {
    if (!handler.connect())
      return null;
    
    String data;
    try {
    
      handler.getObjectOutputStream().writeObject(new String("IIndexerRpc"));
      handler.getObjectOutputStream().writeObject(new String("GetClassInfo"));
      
      handler.getObjectOutputStream().writeObject(new String("TClassDescriptor"));
      handler.getObjectOutputStream().writeObject(desc);
      
      TClassDescriptor result = null;
      data = handler.readString();
      if (data.equals("TClassDescriptor")) {
        result = (TClassDescriptor) handler.getObjectInputStream().readObject();
      } else {
        debug.Error("Recieved wrong parameter type:" + data + " expected TClassDescriptor");
        throw new ClassNotFoundException();
      }
      
      return result;
    }
    catch (IOException e) {
    }
    catch (ClassNotFoundException e) {
    }
    
    handler.close();
    return null;
  }; // GetClassInfo
  
}; // IIndexerRpc

